<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/9
 * Time: 14:19
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class AdminRoleModel
{
    const TABLE = 'admin_role';

    /**
     * 获取账号的角色，
     * @param $adminId
     * @return array
     */
    public function getAdminRole($adminId)
    {
        $result = DB::table(self::TABLE)
            ->leftJoin('role as r','r.id','=',self::TABLE.'.roleId')
            ->where('r.status',1)
            ->where('adminId',$adminId)
            ->select('r.id as roleId','r.name as roleName')
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data){
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }

    public function getListByRole(array $roles,array $data){
        return DB::table(self::TABLE)
            ->leftJoin('admin as a','a.id','=',self::TABLE.'.adminId')
            ->whereIn('roleId',$roles)
            ->where(function ($query) use ($data){
                if (!empty($data['projectId'])){
                    $query->where('a.projectId',$data['projectId']);
                }
            })
            ->get()->toArray();
    }
}